<?php

/**
 * ViewMySchedule function
 *
 * Return schedule for specified employee
 *
 * @author Will Zhao <kexinzhao666@gmail.com>
 */
include_once 'UTDBGlobal.php';
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';

/**
 * @param $userName string
 * @param $userPassHash string
 * @return array|null string
 */
function viewMySchedule($userName, $userPassHash) {
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;
    $err = new ErrorCode();
    $array = array();
    if (checkAuthentication($userName, $userPassHash)) {
        $conn = mysqli_connect($dbaddress, $dbuser, $dbpassword, $dbdatabasename);
        $sql = "SELECT roles.roleName, roledate.dateID, roledate.roleID, roledate.startTime, roledate.endTime, roledate.shiftStatus, dates.datestamp 
            FROM employees LEFT JOIN (dates,roledate,account,roles) ON roledate.empID = employees.empID
            AND dates.dateID = roledate.dateID
            AND roledate.empID = account.empID
            AND roles.roleID = roledate.roleID
            WHERE account.accName = '" . $userName . "'";
        $result = $conn->query($sql);
        if ($result) {
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_array()) {
                    $array[] = array(
                        'roleName' => $row['roleName'],
                        'roleID' => $row['roleID'],
                        'dateID' => $row['dateID'],
                        'date' => $row['datestamp'],
                        'startTime' => $row['startTime'],
                        'endTime' => $row['endTime'],
                        'shiftStatus' => $row['shiftStatus']);
                }
                $error = $err::successRead;
            } else {
                $error = $err::failRead;
            }
        } else {
            $error = $err::failRead;
        }
    } else {
        //echo 'unknow user';
        $error = $err::authFailRead;
    }
    array_unshift($array, $error);
    $result->free();
    $conn->close();
    return $array;
}

?>
